package com.springboot.nacos.discovery;

import com.small.nacos.api.exception.NacosException;
import com.small.nacos.api.naming.NamingFactory;
import com.small.nacos.api.naming.NamingService;

public class NacosServiceManager {

    private static NamingService namingService = null;

    private NacosDiscoveryProperties nacosDiscoveryProperties;

    public NamingService getNamingService(NacosDiscoveryProperties nacosDiscoveryProperties) {

        if (null == namingService) {
            createNamingService(nacosDiscoveryProperties);
        }
        return namingService;
    }

    public NamingService createNamingService(NacosDiscoveryProperties nacosDiscoveryProperties) {
        if (null == namingService) {
            synchronized (NacosServiceManager.class) {
                if (null == namingService) {
                    try {
                        namingService = NamingFactory.createNamingService(nacosDiscoveryProperties.getNacosProperties());
                    } catch (NacosException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
        return namingService;
    }
}
